import java.util.Scanner;

// 单例模式
class Singleton {
    private static Singleton instance = new Singleton();

    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
    }

    public void showMessage() {
        System.out.println("来自单例模式的问候！");
    }
}

// 产品接口
interface Shape {
    void draw();
}

// 具体产品类
class Circle implements Shape {
    public void draw() {
        System.out.println("画一个圆形");
    }
}

class Square implements Shape {
    public void draw() {
        System.out.println("画一个方形");
    }
}

// 工厂类
class ShapeFactory {
    public Shape getShape(String shapeType) {
        if (shapeType == null) {
            return null;
        }
        if (shapeType.equalsIgnoreCase("CIRCLE")) {
            return new Circle();
        } else if (shapeType.equalsIgnoreCase("SQUARE")) {
            return new Square();
        }
        return null;
    }
}

// 命令接口
interface Command {
    void execute();
}

// 具体命令类
class LightOnCommand implements Command {
    private Light light;

    public LightOnCommand(Light light) {
        this.light = light;
    }

    public void execute() {
        light.on();
    }
}

class LightOffCommand implements Command {
    private Light light;

    public LightOffCommand(Light light) {
        this.light = light;
    }

    public void execute() {
        light.off();
    }
}

// 接收者类
class Light {
    public void on() {
        System.out.println("灯开了");
    }

    public void off() {
        System.out.println("灯关了");
    }
}

// 调用者类
class RemoteControl {
    private Command command;

    public void setCommand(Command command) {
        this.command = command;
    }

    public void pressButton() {
        command.execute();
    }
}

// 组件接口
interface Beverage {
    String getDescription();
    double cost();
}

// 具体组件类
class Espresso implements Beverage {
    public String getDescription() {
        return "浓缩咖啡";
    }

    public double cost() {
        return 1.99;
    }
}

// 装饰者抽象类
abstract class CondimentDecorator implements Beverage {
    protected Beverage beverage;

    public CondimentDecorator(Beverage beverage) {
        this.beverage = beverage;
    }
}

// 具体装饰者类
class Mocha extends CondimentDecorator {
    public Mocha(Beverage beverage) {
        super(beverage);
    }

    public String getDescription() {
        return beverage.getDescription() + "，摩卡";
    }

    public double cost() {
        return 0.20 + beverage.cost();
    }
}

// 主类
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("选择一种模式：1. 单例模式 2. 工厂模式 3. 命令模式 4. 装饰者模式");
        int choice = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区

        switch (choice) {
            case 1:
                // 单例模式
                Singleton singleton = Singleton.getInstance();
                singleton.showMessage();
                break;
            case 2:
                // 工厂模式
                ShapeFactory shapeFactory = new ShapeFactory();
                System.out.println("输入形状（CIRCLE 或 SQUARE）：");
                String shapeType = scanner.nextLine();
                Shape shape = shapeFactory.getShape(shapeType.toUpperCase());
                if (shape != null) {
                    shape.draw();
                } else {
                    System.out.println("无效的形状类型");
                }
                break;
            case 3:
                // 命令模式
                Light livingRoomLight = new Light();
                Command lightOn = new LightOnCommand(livingRoomLight);
                Command lightOff = new LightOffCommand(livingRoomLight);
                RemoteControl remote = new RemoteControl();

                System.out.println("请选择灯的开关：");
                System.out.println("输入命令（ON 或 OFF）：");
                String command = scanner.nextLine();
                if ("ON".equalsIgnoreCase(command)) {
                    remote.setCommand(lightOn);
                    remote.pressButton();
                } else if ("OFF".equalsIgnoreCase(command)) {
                    remote.setCommand(lightOff);
                    remote.pressButton();
                } else {
                    System.out.println("无效的命令");
                }
                break;
            case 4:
                // 装饰者模式
                Beverage beverage = new Espresso();
                System.out.println("当前咖啡：" + beverage.getDescription() + " ￥" + beverage.cost());

                System.out.println("是否添加摩卡（Y 或 N）：");
                String addMocha = scanner.nextLine();
                if ("Y".equalsIgnoreCase(addMocha)) {
                    beverage = new Mocha(beverage);
                }
                System.out.println("当前咖啡：" + beverage.getDescription() + " ￥" + beverage.cost());
                break;
            default:
                System.out.println("选择无效");
        }
        scanner.close();
    }
}
